<script setup>
import { Plus, Upload } from '@element-plus/icons-vue'
import { ref } from 'vue'
import avatar from '@/assets/default.png'
import { useUserInfoStore } from '@/stores/userInfo'
import { userUpdateAvatarService } from '@/api/user.js'
import { useTokenStore } from '@/stores/token.js'
import { ElMessage } from 'element-plus'

const uploadRef = ref()
const tokenStore = useTokenStore()
const userInfoStore = useUserInfoStore()
const imgUrl = ref(userInfoStore.info.userPic)

const uploadSuccess = (result) => {
  imgUrl.value = result.data
}

const updateAvatar = async () => {
  let result = await userUpdateAvatarService(imgUrl.value)
  ElMessage.success(result.message ? result.message : "修改成功")
  userInfoStore.info.userPic = imgUrl.value
}

import { userInfoService } from '@/api/user.js'
import { onMounted } from 'vue'

const userAvatar = ref('')

const getUserAvatar = async () => {
  let result = await userInfoService()
  userAvatar.value = result.data.userPic
}

onMounted(() => {
  getUserAvatar()
})
</script>

<template>
  <el-card class="page-container transparent-card">
      <template #header>
          <div class="header">
              <span>更换头像</span>
          </div>
      </template>
      <el-row>
          <el-col :span="12">
              <el-upload 
                  ref="uploadRef"
                  class="avatar-uploader" 
                  :show-file-list="false"
                  :auto-upload="true"
                  action="/api/upload"
                  name="file"
                  :headers="{'Authorization':tokenStore.token}"
                  :on-success="uploadSuccess"
                  >
                  <img v-if="imgUrl" :src="imgUrl" class="avatar" />
                  <img v-else :src="avatar" width="278" />
              </el-upload>
              <br />
              <el-button type="primary" :icon="Plus" size="large"  @click="uploadRef.$el.querySelector('input').click()">
                  选择图片
              </el-button>
              <el-button type="success" :icon="Upload" size="large" @click="updateAvatar()">
                  上传头像
              </el-button>
          </el-col>
      </el-row>
  </el-card>
</template>
<style lang="scss" scoped>
.transparent-card {
  background-color: rgba(255, 255, 255, 0.8); /* 半透明背景 */
  backdrop-filter: blur(10px); /* 模糊效果 */
  border-radius: 8px; /* 圆角 */
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* 可选：轻微阴影效果 */
}
.avatar-uploader {
    :deep() {
        .avatar {
            width: 278px;
            height: 278px;
            display: block;
        }

        .el-upload {
            border: 1px dashed var(--el-border-color);
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            transition: var(--el-transition-duration-fast);
        }

        .el-upload:hover {
            border-color: var(--el-color-primary);
        }

        .el-icon.avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 278px;
            height: 278px;
            text-align: center;
        }
    }
}
</style>